package org.hxyjs.dao.impl;

import org.hxyjs.dao.UserDao;
import org.hxyjs.dao.UserDaoForOrder;
import org.hxyjs.entity.User;
import org.hxyjs.mapper.UserMapper;
import org.hxyjs.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("userDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class UserDaoImpl implements UserDao, UserDaoForOrder {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private UserMapper userMapper;
    public List<User>  getAllUsers() throws Exception{ return userMapper.selectByExample(null); }

    @Override
    public User getUserByUno(String uno) throws Exception { return userMapper.selectByPrimaryKey(uno); }

    public List<User> getUsersByPage(PageInfo pageInfo) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount =(int) userMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return userMapper.getUsersByPage(map);
    }

    @Override
    public Integer deleteUser(String uno) { return userMapper.deleteByPrimaryKey(uno); }

    @Override
    public Integer updateUser(User user) { return userMapper.updateByPrimaryKey(user); }

    @Override
    public Integer addUserByUno(User user){
        user.setUno(initId());
        return userMapper.insertSelective(user);
    }

    @Override
    public User getUsersWithOrders(String uno) throws Exception {
        return userMapper.getUsersWithOrders(uno);
    }
    @Override
    public User getAccountByAccountName(String uname) throws Exception {
        return userMapper.getAccountByAccountName(uname);
    }


    public String initId() {
        String maxUno = userMapper.getMaxUno();
        return org.hxyjs.utils.GenNumTools.initId("19", 4, maxUno);
    }
}